home *** CD-ROM | disk | FTP | other *** search
/ Graphics & Sound Program…ng Techniques for the Mac / Graphics and Sound Programming Techniques for the Mac.iso / Goodies / Animation Libraries / GENetRelease2ƒ / GEDemo / Walk.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-06-24  |  3.3 KB  |  121 lines  |  [TEXT/MPS ]

  1. /*
  2.     Walk.c
  3.     
  4.     Animated walking figure on balcony
  5.     
  6.     Copyright 1993 by Al Evans. All rights reserved.
  7.     
  8.     11/8/93
  9.     
  10. */
  11.  
  12. #include "Walk.h"
  13. #include "Motion.h"
  14. #include "SFXCtrlr.h"
  15. #include "SFXProcs.h"
  16.  
  17. #undef COMMERCIALRELEASE
  18.  
  19.  
  20. #ifdef COMMERCIALRELEASE
  21. pascal void DisposeWalker(GEWorldPtr world, GrafElPtr walker)
  22. {
  23.     if (walker->drawData)
  24.         DisposPtr(walker->drawData);
  25.     if (walker->changeData)
  26.         DisposPtr(walker->changeData);
  27. }
  28. #endif
  29.  
  30.  
  31. Boolean LoadBalconyScene(GEWorldPtr world)
  32. {
  33.     GrafElPtr        thisElement;
  34.     Rect            balconyBox;            //Walking figure positioned relative to balcony
  35.     short            elemHeight;
  36.     MParamPtr        walkMotion;
  37.     
  38.     //Get railing of balcony
  39.     thisElement = NewBasicPICT(world, balconyID, balconyPlane, rBalconyPic,
  40.                                 transparent, balconyLeft, balconyTop);
  41.     if (thisElement == nil) return false;
  42.     balconyBox = thisElement->animationRect;
  43.     
  44.     //Get walking figure
  45.     thisElement = NewAnimatedGraphic(world, walkID, walkPlane, rAnimWalk,
  46.                                 transparent, 0, 0, 10);
  47.     if (thisElement == nil) return false;
  48.  
  49. #ifdef COMMERCIALRELEASE
  50.     //Draw walker masked
  51.     thisElement->drawData = MakeMask(&((GrafPtr) thisElement->graphWorld)->portBits);
  52.     thisElement->copyMode = srcCopy;
  53.     
  54.     //Be sure to dispose mask & motion
  55.     SetCleanupProc(world, walkID, DisposeWalker);
  56. #endif    
  57.  
  58.     //Position figure relative to balcony
  59.     elemHeight = thisElement->graphRect.bottom - thisElement->graphRect.top;
  60.     PtrMoveElementTo(world, thisElement, balconyBox.left + ScaleToWorld(world, 20),
  61.                              balconyBox.bottom - ScaleToWorld(world, 10) - elemHeight, false);
  62.     
  63.     //Initialize motion fields -- never collide with top or bottom
  64.     walkMotion = (MParamPtr) NewPtrClear(sizeof(MotionParams));
  65.     InitMotion(walkMotion, 100, 100);
  66.     walkMotion->currMotion.h = 6;
  67.     walkMotion->limitRect.top  = 0;
  68.     walkMotion->limitRect.left = balconyBox.left + ScaleToWorld(world, 5);
  69.     walkMotion->limitRect.bottom = 1000;
  70.     walkMotion->limitRect.right = balconyBox.right - ScaleToWorld(world, 16);
  71.     
  72.     //Initialize figure's walking action
  73.     SetAutoChange(world, walkID, DoWalker, (Ptr) walkMotion, 133);
  74.     
  75.     //And set animation style to "loop"
  76.     ((SeqGraphicPtr) thisElement)->seq = loop;
  77.     
  78.     //Load speed control slider
  79.     thisElement = NewSliderSensor(world, sliderID, sliderPlane, rSliderBkg, 
  80.                     sliderLeft, sliderTop, hSlideSensor, rSliderCtrl);
  81.     if (thisElement == nil) return false;
  82.     SetSliderPercent(world, sliderID, 50);
  83.     SetSensorAction(world, sliderID, AdjustSpeed);
  84.     (void) DoGESFX(world, 'SFX1', thisElement, SFXHWipe, 30, 3000, 40, true, true);
  85.     
  86.     return true;
  87. }
  88.  
  89.  
  90. pascal void DoWalker(GEWorldPtr world, GrafElPtr walker)
  91. {
  92.     MParamPtr    motion;
  93.     GEDirection    collisionDir;
  94.     
  95.     motion = (MParamPtr) walker->changeData;
  96.     collisionDir = CheckLimits(&walker->animationRect, &motion->limitRect);
  97.     if ((collisionDir == left) || (collisionDir == right)) {
  98.         motion->currMotion.h = -motion->currMotion.h;
  99.         SetMirroring(world, walker->objectID, (collisionDir == right), false);
  100.     }
  101.     MoveElement(world, walker->objectID, motion->currMotion.h, motion->currMotion.v);
  102.     BumpFrame(world, walker->objectID);
  103. }
  104.  
  105. pascal void AdjustSpeed(GEWorldPtr world, short newSpeed)
  106. {
  107.     GrafElPtr walker;
  108.     long newIntrvl;
  109.     
  110.     walker = FindElementByID(world, walkID);
  111.     if (walker) {
  112.         newIntrvl = (newSpeed * 240) / 100;
  113.         if (newIntrvl > 0) {
  114.             newIntrvl = 240 - newIntrvl;
  115.             if (newIntrvl < 16)
  116.                 newIntrvl = 16;
  117.         }
  118.         walker->changeIntrvl = newIntrvl;
  119.     }
  120. }
  121.